package com.jinxiangyang.controller;


import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Role;
import com.jinxiangyang.service.RoleService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色模块")
public class RoleController {




    @Autowired
    private RoleService roleService;



    @GetMapping
    @OperateLog(title = "角色模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
    public AjaxResult getInfo(){
        List<Role> list = roleService.list();
        return AjaxResult.success("操作成功",list);
    }



    @PutMapping
    @OperateLog(title = "角色模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
    public AjaxResult updateInfo(@RequestBody Role role){
        return roleService.updateById(role)?AjaxResult.success():AjaxResult.error("修改失败");
    }


    @PostMapping
    @OperateLog(title = "角色模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.INSERT)
    public AjaxResult insertRole(@RequestBody Role role){
        return roleService.save(role)?AjaxResult.success():AjaxResult.error("添加失败");
    }



    @DeleteMapping("/{id}")
    @OperateLog(title = "角色模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.DELETE)
    public AjaxResult deleteRole(@PathVariable("id") Long id){
        return roleService.removeById(id)?AjaxResult.success():AjaxResult.error("删除失败");
    }
}

